package com.googlecode.jyoutubeuploader.validator;

import java.util.Map;

public class UsernameArgumentValidator implements ArgumentValidator {

	private static final String USERNAME_LONG_ARGUMENT = "--username";
	private static final String USERNAME_SHORT_ARGUMENT = "-u";
	private UsernameValidator usernameValidator;

	@Override
	public boolean isSupported(Map<String, String> args) {
		if (args.containsKey(USERNAME_SHORT_ARGUMENT) || args.containsKey(USERNAME_LONG_ARGUMENT)){
			return true;
		}
		return false;
	}

	@Override
	public ValidationResult validate(Map<String, String> args) {
		if (args.containsKey(USERNAME_SHORT_ARGUMENT) && args.containsKey(USERNAME_LONG_ARGUMENT)){
			return BaseValidationResult.newInvalidInstance("The username was supplied twice");
		}
		String shortArgUsername;
		if ((shortArgUsername = args.get(USERNAME_SHORT_ARGUMENT)) != null){
			boolean isShortArgUsernameValid = usernameValidator.validate(shortArgUsername);
			return isShortArgUsernameValid ? BaseValidationResult.VALID : BaseValidationResult.newInvalidInstance("Username is invalid");
		}
		String longArgUsername;
		if ((longArgUsername = args.get(USERNAME_LONG_ARGUMENT)) != null){
			boolean isLongArgUsernameValid = usernameValidator.validate(longArgUsername);
			return isLongArgUsernameValid ? BaseValidationResult.VALID : BaseValidationResult.newInvalidInstance("Username is invalid");
		}
		return BaseValidationResult.VALID;
	}

	public void setUsernameValidator(UsernameValidator usernameValidator) {
		this.usernameValidator = usernameValidator;
	}
}
